package com.cnswhy.cloud.server.core.service.merchant.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.core.dao.MerchantMapper;
import com.cnswhy.cloud.server.core.dto.MerchantInfoVo;
import com.cnswhy.cloud.server.core.entity.Merchant;
import com.cnswhy.cloud.server.core.entity.MerchantSetting;
import com.cnswhy.cloud.server.core.entity.MerchantSettingSlider;
import com.cnswhy.cloud.server.core.service.MerchantSettingService;
import com.cnswhy.cloud.server.core.service.MerchantSettingSliderService;
import com.cnswhy.cloud.server.core.service.merchant.MerchantService;
import com.cnswhy.cloud.common.core.base.service.SuperServiceImpl;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 商户基本信息
 * </p>
 *
 * @author cnswhy
 * @date 2020-09-30
 */
@Slf4j
@Service

public class MerchantServiceImpl extends SuperServiceImpl<MerchantMapper, Merchant> implements MerchantService {
    /*@Override
    public MerchantInfoVo queryMerchantVo(Long id) {
        MerchantInfoVo queryMerchantAppVo = getBaseMapper().queryMerchantVo(id);
        return queryMerchantAppVo;
    }*/
    @Autowired
    MerchantSettingService merchantSettingService;
    @Autowired
    MerchantSettingSliderService merchantSettingSliderService;
    @Override
    public JSONObject getMerchantDetail(Long id) {
        Merchant merchant = getById(id);
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(merchant),JSONObject.class);
        MerchantSetting merchantSetting = merchantSettingService.getOne(Wraps.<MerchantSetting>lbQ().eq(MerchantSetting::getMerchantId,merchant.getId()));
        if(merchantSetting != null){
            jsonObject.put("logoImg",merchantSetting.getLogoUrl());
            jsonObject.put("backgroundImg",merchantSetting.getBackgroundUrl());
            jsonObject.put("posterImg",merchantSetting.getPosterUrl());
        }
        List<MerchantSettingSlider> merchantSettingSliders = merchantSettingSliderService.list(Wraps.<MerchantSettingSlider>lbQ().eq(MerchantSettingSlider::getMerchantId,merchant.getId()));
        List<String> sliderImgList = new ArrayList<>();
        for(MerchantSettingSlider merchantSettingSlider:merchantSettingSliders){
            sliderImgList.add(merchantSettingSlider.getUrl());
        }
        if(sliderImgList.size() > 0){
            jsonObject.put("sliderImgList",sliderImgList);
        }
        return jsonObject;
    }
}
